package io.github.luons.mediator.core.spi.result;

import lombok.Data;

import java.io.Serializable;

/**
 * api result
 *
 * @author : luons
 */
@Data
public class ApiResult<T> extends BaseResult<T> implements Serializable {

    private T result;

    private Error error;

    private String requestId;

    public ApiResult() {
        this.error = new Error();
    }

    public ApiResult(T result) {
        this.error = new Error();
        this.result = result;
    }

    public static <T> ApiResult<T> success() {
        return new ApiResult<>();
    }

    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<>(data);
    }

    public static <T> ApiResult<T> error(Integer code) {
        ApiResult<T> result = new ApiResult<>();
        result.setError(new Error(code));
        return result;
    }

    public static <T> ApiResult<T> error(Integer code, String message) {
        ApiResult<T> result = new ApiResult<>();
        result.setError(new Error(code, message));
        return result;
    }

    @Data
    public static class Error {

        private Integer code = 0;
        private String message = "";

        public Error() {

        }

        public Error(Integer code) {
            this.code = code;
        }

        public Error(Integer code, String message) {
            this.code = code;
            this.message = message;
        }
    }

}
